<?php
namespace app\api\controller;

use think\Request;
use app\api\model\Send;

class Index
{

    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');

        header('Access-Control-Allow-Methods:GET,POST,PUT');

        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }
    public function add()
    {
        $post = Request::instance()->get();
        if (empty($post)){
            return json(Rearr('',-201,'success'));
        }
        $post['add_time'] = time();
        foreach ($post as $key => $val){
            $post[$key] = htmlentities($val);
        }
        $add = db('linshi') -> insert($post);
        if ($add){
            return json(Rearr('',200,'success'));
        }else{
            return json(Rearr('',-201,'error'));
        }
    }
    //直接新增客户client_name客户姓名client_phone手机号uuid
    public function add_kehu()
    {
        $post = Request::instance()->post();
        if (!isset($post['client_name'])){
            return json(Rearr('',-1,'error'));
        }
        if (!isset($post['client_phone'])){
            return json(Rearr('',-2,'error'));
        }
        //判断uuid
        if (!isset($post['uuid'])){
            $post['uuid'] = "dfeaa996-b03e-c7e2-e72c-1605a25b5038";
            $post['shangwu_name'] = "郭总";
        }else{
            $sel = db('busines') -> where('uuid',$post['uuid']) -> find();
            if ($sel){//如果存在
                $post['shangwu_name'] = $sel['shangwu_name'];
                $post['uuid'] = $sel['uuid'];
            }else{//不存在
                $post['uuid'] = "dfeaa996-b03e-c7e2-e72c-1605a25b5038";
                $post['shangwu_name'] = "郭总";
            }
        }
        $sel = db('clents') -> where('client_phone',$post['client_phone']) -> find();
        if ($sel){//有该客户
            //判断是否领过红包
            if (empty($sel['money'])){//没有领过红包
                return json(Rearr('',-100,'error'));
            }else{//领过红包
                //判断姓名是否已输入
                if (empty($sel['client_name'])){
                    if (db('clents') -> where('client_phone',$post['client_phone']) -> update(['client_name'=>$post['client_name']])){
                        return json(Rearr('',200,'success'));//姓名存入成功
                    }else{
                        return json(Rearr('',-201,'error'));//姓名存入失败
                    }
                }else{//姓名已输入
                    return json(Rearr('',-100,'error'));
                }
            }
        }else{//没有该客户
            $post['create_time'] = time();
            if (db('clents') -> insert($post)){
                return json(Rearr('',200,'success'));//信息存入成功
            }else{
                return json(Rearr('',-201,'error'));//信息存入失败
            }
        }
    }
    //领红包新增用户
    public function add_money()
    {
        $post = Request::instance()->post();
        if (!isset($post['client_phone'])){
            return json(Rearr('',-2,'error'));
        }else{
            //判断uuid
            if (!isset($post['uuid'])){
                $post['uuid'] = "dfeaa996-b03e-c7e2-e72c-1605a25b5038";
                $post['shangwu_name'] = "郭总";
            }else{
                $sel = db('busines') -> where('uuid',$post['uuid']) -> find();
                if ($sel){//如果存在
                    $post['shangwu_name'] = $sel['shangwu_name'];
                    $post['uuid'] = $sel['uuid'];
                }else{//不存在
                    $post['uuid'] = "dfeaa996-b03e-c7e2-e72c-1605a25b5038";
                    $post['shangwu_name'] = "郭总";
                }
            }

            if (empty($post['client_phone'])){
                return json(Rearr('',-2,'error'));
            }
            //判断是否存在
            $sel = db('clents')->where('client_phone',$post['client_phone'])->find();
            if ($sel){//存在
                //判断是否领过红包
                if($sel['money']==0){//没有领过
                    if (db('clents')->where('client_phone',$post['client_phone'])->update(['money'=>$post['money']])){//给用户存入红包
                        return json(Rearr('',200,'success'));//存入红包
                    }else{
                        return json(Rearr('',-201,'error'));//存入失败
                    }
                }else{
                    return json(Rearr('',-4,'error'));//领过红包
                }
            }else{
                $post['create_time'] = time();
                if (db('clents') -> insert($post)){
                    //$code = $post['money']."元";
                    //$send = new Send();
                    //$send->sms($code,$post['client_phone']);
                    return json(Rearr('',200,'success'));//信息存入成功
                }else{
                    return json(Rearr('',-201,'error'));//信息存入失败
                }
            }
        }
    }
}
